import Vue from 'vue'
import Router from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/')
    },
    { 
      path:'/',
      component:() => import('@/views/layout/'),
      children:[
        { 
          path:'',
          name:'home',
          component:() => import('@/views/home')
        },
        { 
          path:'/artifact',
          name:'artifact',
          component:() => import('@/views/artifact')
        },
        { 
          path:'/news',
          name:'news',
          component:() => import('@/views/news')
        },
        { 
          path:'/user',
          name:'user',
          component:() => import('@/views/user')
        }
      ]
    }
  ]
})

//进度条
NProgress.configure({
  easing: 'ease',  // 动画方式
  speed: 500,  // 递增进度条的速度
  showSpinner: false, // 是否显示加载ico
  trickleSpeed: 200, // 自动递增间隔
  minimum: 0.3 // 初始化时的最小百分比
})

router.beforeEach((to,from,next) => { 
    NProgress.start()
    const result = JSON.parse(window.localStorage.getItem("result"))

    if(to.path != '/login'){ 
        if(result){ 
          next()
        } else { 
          next('/login')
        }

    } else { 
      next()
    }
})

router.afterEach(() => {
  // 在即将进入新的页面组件前，关闭掉进度条
  NProgress.done()
})

export default router
